package com.example.myapplication.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 水波纹特效
 */
public class VoiceWaveView extends View {
    private boolean mIsRunning;

    public VoiceWaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void start() {
        if (!mIsRunning) {
            mIsRunning = true;
            setVisibility(View.VISIBLE);
            ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 1f, 0.1f).setDuration(800);
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(this, "scaleX", 1, 1.3f).setDuration(800);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(this, "scaleY", 1, 1.3f).setDuration(800);
            alpha.setRepeatCount(2);
            scaleX.setRepeatCount(2);
            scaleY.setRepeatCount(2);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(alpha, scaleX, scaleY);
            animatorSet.setDuration(800);
            animatorSet.setInterpolator(new LinearInterpolator());
            animatorSet.start();

        }
        mHandler.removeMessages(1);
        mHandler.sendEmptyMessageDelayed(1, 1581);
    }

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                stop();
            }
        }
    };

    public void stop() {
        mHandler.removeMessages(1);
        setVisibility(View.INVISIBLE);
        mIsRunning = false;
    }
}